package user

import (
	"github.com/gin-gonic/gin"
	"k8s-dash/dto"
	"k8s-dash/middleware"
	"k8s-dash/service/user"
	"k8s-dash/utils"
	"k8s-dash/utils/logger"
)

type userController struct{}

var UserController userController

func (p *userController) Register(ginRouter *gin.RouterGroup) {
	nd := &userController{}
	ginRouter.POST("/user/login", nd.Login)
}

// Login 登录
// ListPage godoc
// @Summary      登录
// @Description  登录
// @Tags         user
// @ID           /api/user/login
// @Accept       json
// @Produce      json
// @Param        username         query  string     true  "用户名"
// @Param        password        query  string     true  "密码"
// @Success      200 {object}  middleware.Response"{"code": 200, msg="","data": string}"
// @Router       /api/user/login [post]
func (u *userController) Login(ctx *gin.Context) {
	parmas := &dto.UserInput{}
	//参数验证
	err := parmas.BindValidate(ctx)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.ParamBindError))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.ParamBindError, err))
		return
	}
	token, err := user.User.Login(parmas.Username, parmas.Password)
	if err != nil {
		logger.LG.Error(utils.GetErrorMsg(utils.LoginErr))
		middleware.ResponseError(ctx, utils.NewGlobalError(utils.LoginErr, err))
		return
	}
	middleware.ResponseSuccess(ctx, &dto.UserToken{Token: token})
}
